package leetcode;

public class LeetCode108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        int mid = (nums.length - 1) / 2, leftIndex = mid - 1, rightIndex = mid + 1;
        boolean leftFinish = false, rightFinish = false;
        TreeNode root = new TreeNode(nums[0]), leftNode = root, rightNode = root;
        while (leftFinish && rightFinish) {
            if (leftIndex < 0) {
                leftFinish = true;
            } else {
                leftNode.left = new TreeNode(nums[leftIndex]);
                leftNode = leftNode.left;
                leftIndex--;
            }

            if (rightIndex > nums.length) {
                rightFinish = true;
            } else {
                rightNode.right = new TreeNode(nums[rightIndex]);
                rightNode = rightNode.right;
                rightIndex++;
            }
        }
        return root;
    }
//------------------------------------------------------------------
}
